const db = uniCloud.database()
const dbCmd = db.command

function refreshToken({
	token,
	tokenExpired
}) {
	uni.setStorageSync('uni_id_token', token)
	uni.setStorageSync('uni_id_token_expired', tokenExpired)
}
// 绑定刷新token事件
db.on('refreshToken', refreshToken)

function onDBError({
	code, // 错误码详见https://uniapp.dcloud.net.cn/uniCloud/clientdb?id=returnvalue
	message
}) {
	if (code.indexOf('TOKEN') > -1) {
		uni.navigateTo({
			url: '/pages/user/login.vue'
		})
	}
	uni.showModal({
		title: message
	})
}
// 绑定clientDB错误事件
db.on('error', onDBError)

const install = (Vue) => {
	Vue.prototype.$db = db
	Vue.prototype.$dbCmd = dbCmd
}

export default {
	install
}
